package com.example.guet_assistant.controller;
import cn.hutool.core.bean.BeanUtil;
import com.example.guet_assistant.domain.dto.UserAccountBindingDTO;
import com.example.guet_assistant.domain.dto.UserFromDTO;
import com.example.guet_assistant.domain.dto.WxLoginDTO;
import com.example.guet_assistant.domain.po.User;
import com.example.guet_assistant.domain.vo.UserLoginVO;
import com.example.guet_assistant.domain.vo.UserInfoVO;
import com.example.guet_assistant.properties.JwtProperties;
import com.example.guet_assistant.result.Result;
import com.example.guet_assistant.service.IUserService;
import com.example.guet_assistant.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-04
 */
@Tag(name="用户管理接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService  iUserService;

    @Autowired
    private JwtProperties jwtProperties;

    @Operation(summary = "用户登录")
    @PostMapping("/login/{studentId}/{password}")
    public Result<UserLoginVO> userLogin(@PathVariable("studentId") Long studentId , @PathVariable("password") String password  ){
        Map<String, Object> claims =new HashMap<>();
        Integer userId = iUserService.login(studentId, password);
        claims.put("userId", userId);
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        return Result.success(new UserLoginVO(userId,token));
    }

    @Operation(summary = "基于微信登录实现小程序登录")
    @PostMapping("/login")
    public Result<UserLoginVO> userLogin(@RequestBody WxLoginDTO wxLoginDTO){
        Map<String, Object> claims =new HashMap<>();
        Integer userId = iUserService.login(wxLoginDTO);
        claims.put("userId",userId);
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        return Result.success(new UserLoginVO(userId,token));
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public  Result  userRegister(@RequestBody UserFromDTO userFromDTO){
        iUserService.register(userFromDTO);
        return Result.success();
    }


    @Operation(summary = "根据id获取用户信息")
    @GetMapping("{id}")
    public  Result<UserInfoVO> getUserInfo(@PathVariable("id") Integer id){
        return Result.success(BeanUtil.copyProperties(iUserService.getById(id), UserInfoVO.class));
    }


    @Operation(summary = "根据id修改用户信息")
    @PutMapping
    public  Result    updateUserInfo(@RequestBody User user){
        iUserService.updateById(user);
        return Result.success();
    }


    @Operation(summary = "用户账号绑定")
    @PutMapping("/binding")
    public  Result   userAccountBinding(@RequestBody UserAccountBindingDTO  userAccountBindingDTO){
        iUserService.userAccountBinding(userAccountBindingDTO);
        return Result.success();
    }


    @Operation(summary = "返回用户列表")
    @GetMapping("/getUserList")
    public Result<Map<Integer, String>> getUserList(){
        return iUserService.getUserList();
    }
}
